/**
 * Created with IntelliJ IDEA.
 * User: Katerina
 * Date: 23.01.13
 * Time: 2:47
 * To change this template use File | Settings | File Templates.
 */
package ru.gameChangers.homework.businessModels {
import flash.display.Loader;
import flash.events.Event;
import flash.net.URLRequest;

import org.osmf.image.ImageLoader;

public class ContentItemsContainer {

    private var arr:Array = new Array();
    private var titleLogo:ContentItem;
    private var callback:Function = null;

    public function ContentItemsContainer(titleLogo:ContentItem) {
        this.titleLogo = titleLogo;
    }

    public function setCallback(callback:Function) : void {
        this.callback = callback;
        for (var i:int = 0; i < arr.length; ++i) {
            callback(arr[i]);
        }
    }

    public function addNewItem(item:ContentItem) : void {
        var loader:Loader = new Loader();
        var request:URLRequest = new URLRequest(item.getLogoUrl());
        loader.load(request);
        loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event) : void {
            item.logo = loader;
            if (callback == null) {
                arr.push(item);
            } else {
                callback(item);
            }

        });
    }

    public function getTitleLogo() : ContentItem {
        return this.titleLogo;
    }

}
}
